package com.rmb.userinfo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.rmb.userinfo.dao.LogAddressDao;
import com.rmb.mart.entity.log.LogAddress;
import com.rmb.userinfo.excal.LogAddressExcal;
import com.rmb.userinfo.service.LogAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (LogAddressExcal)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 11:03:51
 */
@Service("logAddressService")
public class LogAddressServiceImpl implements LogAddressService {
    @Resource
    private LogAddressDao logAddressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LogAddress queryById(Long id) {
        return this.logAddressDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogAddress> queryAllByLimit(int offset, int limit) {
        return this.logAddressDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logAddress 实例对象
     * @return 实例对象
     */
    @Override
    public LogAddress insert(LogAddress logAddress) {
        this.logAddressDao.insert(logAddress);
        return logAddress;
    }

    /**
     * 修改数据
     *
     * @param logAddress 实例对象
     * @return 实例对象
     */
    @Override
    public LogAddress update(LogAddress logAddress) {
        this.logAddressDao.update(logAddress);
        return this.queryById(logAddress.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.logAddressDao.deleteById(id) > 0;
    }

    /**
     * 查询所有日志记录
     * @return
     */
    @Override
    public List selectAll() {
        return logAddressDao.selectAll();
    }

    /**
     * 导出Excal表
     */
    @Override
    public boolean exportExcal() {
        //相对路径
        String path = Thread.currentThread().getContextClassLoader()
                .getResource("com/rmb/entity/files").toString();
        List list = logAddressDao.selectAll();
        ExcelWriterBuilder workBook = EasyExcel.write(path, LogAddressExcal.class);
        workBook.sheet().doWrite(list);
        return true;
    }
}